<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Tweet
 * @description : View, Create, Edit tweet
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Tweet extends AUTH_Controller {

  /**
   * View a tweet
   * 
   */
  public function index($tweet_id = NULL) {

    // Getting & Cleaning inputs
    $reply_limit = $this->input->get('limit', TRUE);
    $reply_limit = (int) (!empty($reply_limit)) ? $reply_limit : 10;
    $reply_sort = $this->input->get('sort', TRUE);
    $reply_sort = (int) ($reply_sort == 2) ? $reply_sort : 1;

    // Checking tweet id
    if (!empty($tweet_id) && is_numeric($tweet_id)) {

      // Getting tweet data
      $get_tweet_by_id = $this->tweet_model->get_tweet_by_id($tweet_id);
      $tweet_data = $get_tweet_by_id['result'];

      // Checking tweet data
      if ($get_tweet_by_id['status'] == 1 && !empty($get_tweet_by_id['result'])) {

        // Getting tweet replies
        $get_tweet_reply_by_id = $this->tweet_model->get_tweet_reply_by_id($tweet_id, $reply_limit, $reply_sort);
        $tweet_reply_data = $get_tweet_reply_by_id['result'];

        // Getting tweet users
        $get_tweet_users = $this->tweet_model->get_tweet_users($tweet_id);
        $tweet_users_data = $get_tweet_users['result'];

        // Getting tweet extras
        $get_tweet_extras = $this->tweet_model->get_tweet_extras($tweet_id, $this->current_user['user_id']);
        $tweet_extras_data = $get_tweet_extras['result'];

        // Getting tweet follows
        $get_follow = $this->tweet_model->get_follow($tweet_id, $this->current_user['user_id']);
        $tweet_follow_data = $get_follow['result'];

        // Getting tweet followers
        $get_followers = $this->tweet_model->get_followers($tweet_id);
        $tweet_followers_count = $get_followers['count'];

        // Getting tweet viewers
        $tweet_viewers = @tweet_viewers($tweet_id);

        // Getting last newly loaded reply
        $get_tweet_reply_by_id_new = $this->tweet_model->get_tweet_reply_by_id($tweet_id, 1, 1);

        // Preparing ids for load more & auto load
        if (!empty($get_tweet_reply_by_id_new['result'])) {
          $last_reply_id = $get_tweet_reply_by_id_new['result'][0]['tweet_reply_id'];
          $reply_array_end = end($get_tweet_reply_by_id['result']);
          $last_loaded_reply_id = $reply_array_end['tweet_reply_id'];
        } else {
          $last_reply_id = 1;
          $last_loaded_reply_id = 1;
        }

        // Preparing view data
        $view_data = array();
        $view_data['menu_active'] = 'tweets';
        $view_data['tweet_data'] = $tweet_data;
        $view_data['last_reply_id'] = $last_reply_id;
        $view_data['last_loaded_reply_id'] = $last_loaded_reply_id;
        $view_data['reply_sort'] = $reply_sort;
        $view_data['tweet_reply_data'] = $tweet_reply_data;
        $view_data['tweet_users_data'] = $tweet_users_data;
        $view_data['tweet_follow_data'] = $tweet_follow_data;
        $view_data['tweet_followers_count'] = $tweet_followers_count;
        $view_data['tweet_extras_data'] = $tweet_extras_data;
        $view_data['reply_limit'] = $reply_limit;
        $view_data['tweet_viewers'] = $tweet_viewers;

        // Loading view
        $this->load_view('tweet', $view_data);
        return;
      }
    }

    // Redirect to tweets if something wrong
    redirect('tweets');
    return;
  }

  /**
   * Create a new tweet
   * 
   */
  public function create() {

    // Init variables
    $new_tweet_status = NULL;
    $tweet_subject = NULL;
    $tweet_message = NULL;

    // Checking request type is post
    if ($this->is_post()) {

      // Setting tweet create status flag
      $new_tweet_status = 2;

      // Cleaning inputs
      $tweet_subject = $this->sanitize($this->input->post('tweet_subject', TRUE));
      $tweet_message = $this->sanitize_html($this->input->post('tweet_message', TRUE));

      // Checking inputs
      if (!empty($tweet_subject) && !empty($tweet_message)) {

        // Inserting tweet
        $set_tweet = $this->tweet_model->set_tweet($tweet_subject, $tweet_message, $this->current_user['user_id']);

        // Checking insert status
        if ($set_tweet['status'] == 1 && !empty($set_tweet['result'])) {

          // Setting tweet create status flag to success
          $new_tweet_status = 1;

          // Set the owner as follower of tweet
          $this->tweet_model->add_follow($set_tweet['result']['tweet_id'], $this->current_user['user_id']);

          // Setting session values for tweet creation status & notification sending
          $this->session->set_flashdata('new_tweet_status', $new_tweet_status);
          $this->session->set_flashdata('tweet_tweet_id', $set_tweet['result']['tweet_id']);
          $this->session->set_flashdata('tweet_tweet_hash', md5($tweet_message));

          // Redirect to newly created tweet tweet
          redirect('tweet/' . $set_tweet['result']['tweet_id']);
          return;
        }
      }

      // Redirect to self, if someting goes wrong
      $this->session->set_flashdata('new_tweet_status', $new_tweet_status);
      redirect('tweet/create');
      return;
    }

    // Preparing view
    $view_data = array();
    $view_data['menu_active'] = 'tweets';
    $view_data['tweet_subject'] = $tweet_subject;
    $view_data['tweet_message'] = $tweet_message;

    // Load view
    $this->load_view('tweet_create', $view_data);
    return;
  }

  /**
   * Update a tweet (Edit)
   * 
   */
  public function update($tweet_id = NULL) {

    // Init variables
    $update_tweet_status = NULL;
    $tweet_subject = NULL;
    $tweet_message = NULL;

    // Checking tweet id
    if (!empty($tweet_id) && is_numeric($tweet_id)) {

      // Getting tweet
      $get_tweet_by_id = $this->tweet_model->get_tweet_by_id($tweet_id);

      // Checking tweet
      if ($get_tweet_by_id['status'] == 1 && !empty($get_tweet_by_id['result'])) {

        // Preparing tweet data
        $tweet_data = $get_tweet_by_id['result'];
        $tweet_subject = $tweet_data['tweet_subject'];
        $tweet_message = $tweet_data['tweet_message'];

        // Checking tweet ownership
        if ($tweet_data['tweet_user_id'] == $this->current_user['user_id'] || ($this->is_admin)) {

          // Checking request is post
          if ($this->is_post()) {

            // Setting status
            $update_tweet_status = 2;

            // Getting & Cleaning inputs
            $tweet_subject = $this->sanitize($this->input->post('tweet_subject', TRUE));
            $tweet_message = $this->input->post('tweet_message', TRUE);

            // Checking inputs
            if (!empty($tweet_subject) && !empty($tweet_message)) {

              // Updating tweet
              $update_tweet_by_id = $this->tweet_model->update_tweet_by_id($tweet_id, $tweet_subject, $tweet_message);

              // Checking update
              if ($update_tweet_by_id['status'] == 1 && !empty($update_tweet_by_id['result'])) {

                // Setting success status
                $update_tweet_status = 1;
                $this->session->set_flashdata('update_tweet_status', $update_tweet_status);

                // Redirect back to tweet
                redirect('tweet/' . $tweet_id);
                return;
              }
            }
            $this->session->set_flashdata('update_tweet_status', $update_tweet_status);
            redirect('tweet/update/' . $tweet_id);
            return;
          }

          // Preparing view
          $view_data = array();
          $view_data['menu_active'] = 'tweets';
          $view_data['tweet_id'] = $tweet_id;
          $view_data['tweet_subject'] = $tweet_subject;
          $view_data['tweet_message'] = $tweet_message;

          // Loading view
          $this->load_view('tweet_update', $view_data);
          return;
        }
      }
    }
    redirect('tweets');
    return;
  }

  /**
   * Delete a tweet
   * 
   */
  public function delete($tweet_id = NULL) {

    // Checking tweet id
    if (!empty($tweet_id) && is_numeric($tweet_id)) {

      // Deleting tweet (Setting status to 0)
      $delete = false;
      if ($this->is_admin) {
        $delete = true;
        $this->tweet_model->delete_tweet($tweet_id);
      } else {
        $delete = true;
        $this->tweet_model->del_tweet_by_id($tweet_id, $this->current_user['user_id']);
      }

      if ($delete) {
        $this->like_model->del_likes_by_post('tweet', $tweet_id);
        $get_tweet_reply_by_id = $this->tweet_model->get_tweet_reply_by_id($tweet_id, null);
        if (!empty($get_tweet_reply_by_id['result'])) {
          foreach ($get_tweet_reply_by_id['result'] as $reply) {
            $this->tweet_model->delete_reply($reply['tweet_reply_id']);
            $this->like_model->del_likes_by_post('reply', $reply['tweet_reply_id']);
          }
        }
      }
    }

    // Redirect to tweets list
    redirect('tweets');
    return;
  }

}

?>